//
//  MainViewController.swift
//  weibo
//
//  Created by sanya on 15/6/23.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    
        // 添加控制器
        addChildViewControllers()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        let w = tabBar.bounds.width / CGFloat(5)
        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2*w, 0)
    }
    
    /// 点击撰写按钮，按钮监听方法不能私有
    func composedButtonClicked(){
        let vc = ComposeViewController()
        presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
    }
    /// 添加所以子控制器
    private func addChildViewControllers() {

             // 捕获异常
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController(), title: "", imageName: nil)
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc:UIViewController,title:String,imageName:String?) {
    
        // 设置tintColor
//        tabBar.tintColor = UIColor.orangeColor()
        
        vc.title = title
        if imageName != nil {
            vc.tabBarItem.image = UIImage(named: imageName!)
            vc.tabBarItem.selectedImage = UIImage(named: imageName! + "_highlighted")
        }
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    
    }
    /// 撰写按钮
    private lazy var composeButton:UIButton = {
        var btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "composedButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(btn)
    
        return btn
    }()

}
